const { default: DPagination } = require("../../../utils/DPagination");
const { getProjectNameLang } = require("../common");
var location = require("../../../mybehaviors/location/location");


// packageH/project_verification/CategoryProjectList/CategoryProjectList.js
const App = getApp();
const ProjectPagination = new DPagination("projects");
const getProjectsUrl = App.getNetAddresss("plugin.store-projects.frontend.project.get-list");
let Location = null;
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    categoryId: null,
    projects: {},
    projectCount: 0,
    projectNameLang: getProjectNameLang(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    wx.setNavigationBarTitle({
      title: getProjectNameLang() + "列表",
    });
    if (!options.category_id) {
      wx.showToast({
        title: "分类不存在",
        icon: "none",
      });
      return;
    }
    ProjectPagination.bind(this);
    ProjectPagination.clean();
    this.setData({
      categoryId: options.category_id,
    });

    this._getLocation((res,point) => {
      Location = {
        point: point
      };
      this.getProjects();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    ProjectPagination.clean();
    this.getProjects();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getProjects();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: "/packageH/project_verification/CategoryProjectList/CategoryProjectList?category_id=" +  this.data.categoryId,
    };
  },
  onShareTimeline() {
    return {
      query: "project_id=" +  this.data.categoryId,
    };
  },
  getProjects() {
    if (ProjectPagination.loading || ProjectPagination.finished) {
      return;
    }
    ProjectPagination.loading = true;
    let point = App.qqMapTransBMap(parseFloat(Location.point.lng), parseFloat(Location.point.lat));
    App._getNetWork({
      url: getProjectsUrl,
      data: {
        page: ProjectPagination.loadPage,
        category_id: this.data.categoryId,
        lng: point.lng,
        lat: point.lat,
      },
      success: ({ data: { data: response } }) => {
        if (response.current_page == response.last_page || response.data.length < response.per_page) {
          ProjectPagination.finished = true;
        }
        ProjectPagination.push(response.data);
        ProjectPagination.loading = false;
        wx.stopPullDownRefresh();
        this.setData({
          projectCount: ProjectPagination.length,
        });
      },
      fail() {
        ProjectPagination.loading = false;
        wx.stopPullDownRefresh();
      },
    });
  },
});
